<?php 

class MY_Controller extends Controller
{
	var $page;
	var $pagePermissions=array();
	var $permissionsObject;
	var $module_codes;
	public function __construct()
	{
		parent::__construct();

		
		//  load libs
		
		
		$this->load->model('staff_type_model' , 'STM');	
	//
		$this->load->library('Permissions');
		// set groupID
		$this->permissionsObject=new Permissions();
		$groupID = ($this->session->userdata('group_id')) ? $this->session->userdata('group_id') : 0;
		//die("$this->page,$groupID");
		$this->pagePermissions=$this->permissionsObject->get_pages_permissions($this->page,$groupID);
		
		if (!$this->session->userdata('logged_in'))
		{
			redirect('login/' , 'refresh');
		}
		
		//var_dump($this->session->userdata('version'));
		if(!$this->session->userdata('version')){
			//Updater::update();
			 
			require_once(dirname(__FILE__).'/Updater.php');
			$update_obj = new Updater(array('server_url'=>$this->config->item('remote_update_server')));
			$update_obj->update();
			//var_dump($update_obj->getVersion());exit;
			$this->session->set_userdata( 'version', $update_obj->getVersion() );
			 
		}
		
		// get permissions and show error if they don't have any permissions at all
		$this->load->model('modules_model' , 'MM');	
		//print_r($this->MM->get_modules());
		$modules =$this->MM->get_modules();
		foreach($modules['info'] as $module){
			$this->module_codes[$module['name']]=$module['id'];
		}
		
		
	}
	/*
	 * $type:access,add,edit
	 * 
	 */
	function check_page_permission($type)
	{
		return true;
		$username= ($this->session->userdata('username')) ? $this->session->userdata('username') : 0;
		if(!strcmp($username,'root')) return true;
		$permission_type=$this->permissionsObject->permission_codes[$type];		
		$groupID = ($this->session->userdata('group_id')) ? $this->session->userdata('group_id') : 0;
			if (empty($this->pagePermissions[$groupID]))
		{
			show_error('You do not have any permissions!');
		}
	
		// get permissions and show error if they don't have any permissions at all
		
		if (empty($this->pagePermissions[$groupID][$this->page][$permission_type])){
			show_error('You do not have '.$type.'  permissions!');
		}
	}

	
}
